package org.liveboardgames.agricola.business;

import java.util.ArrayList;
import java.util.List;

import org.liveboardgames.agricola.access.ICardDao;
import org.liveboardgames.agricola.access.IFarmDao;
import org.liveboardgames.agricola.business.updater.FarmUpdater;
import org.liveboardgames.agricola.controller.AgricolaUpdateAdapter;
import org.liveboardgames.agricola.domain.Change;
import org.liveboardgames.agricola.domain.card.Oven;
import org.liveboardgames.agricola.domain.farm.Farm;
import org.liveboardgames.agricola.model.enums.ResourceTypeEnum;

public class BakeServiceImpl implements IBakeService {

  private IFarmDao farmDao;

  private ICardDao cardDao;

  public void setCardDao(ICardDao cardDao) {
    this.cardDao = cardDao;
  }

  public void setFarmDao(IFarmDao farmDao) {
    this.farmDao = farmDao;
  }

  public void bake(Long gameId, Long farmId, Integer numberOfGrains, Long ovenId) {
    Farm farm = farmDao.load(farmId);

    Oven oven = (Oven)cardDao.load(ovenId);

    List<Change> changes = new ArrayList<Change>();
    new FarmUpdater(null, farm, changes).addResource(ResourceTypeEnum.FOOD, numberOfGrains * oven.getFoodPerUse());
    new FarmUpdater(null, farm, changes).addResource(ResourceTypeEnum.GRAIN, -numberOfGrains);

    farmDao.save(farm);

    new AgricolaUpdateAdapter().publishGameUpdates(gameId, changes);

  }

}
